package me.peng.rest;

import me.peng.domain.Comment;
import me.peng.domain.Recyclerhome;
import me.peng.domain.config.SysResult;
import me.peng.service.FileService;
import me.peng.service.vo.ImageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import me.peng.annotation.rest.AnonymousDeleteMapping;
import me.peng.annotation.rest.AnonymousPostMapping;

import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/api")
public class FileController {

    //轮播管理+商品封面两个进行复用
    public static String urlPath=null;
    //动态列表图片
    public static String list="";
    //商品轮播
    public static String commdityCarousel="";
    //商品详情
    public static String commdityDtails="";

    public static String header="";
    public static String body="";
    public static String tail="";



    @Autowired
    FileService fileService;
    /**
     * 业务: 实现文件上传
     * url: /file/upload
     * 请求类型: POST
     * 参数: file
     * 返回值: SysResult(imageVO)
     * 高级API:MultipartFile 自动维护了缓存流/自动开关
     *
     * 文件上传步骤:
     *      1.获取文件名称.
     *      2.准备上传文件的目录
     *      3.封装文件全路径  目录/文件名称
     *      4.实现文件上传
     */
    @AnonymousPostMapping("/upload")
    public SysResult upload(MultipartFile file) throws IOException {

        ImageVO imageVO = fileService.upload(file);

        //说明业务执行有误
        if(imageVO != null){
            //获取网络地址
            urlPath=imageVO.getUrlPath();
        }else {
            return SysResult.fail();
        }

        return SysResult.success(imageVO);

    }


    /***
     * 动态信息展示多个图片资源文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @AnonymousPostMapping("/uploadfind")
    public SysResult uploadFind(MultipartFile file) throws IOException {

        ImageVO imageVO = fileService.upload(file);

        //说明业务执行有误
        if(imageVO != null){
            //获取网络地址
            list=list+imageVO.getUrlPath()+",";
        }else {
            return SysResult.fail();
        }

        return SysResult.success(imageVO);

    }

    /***
     *实现商品轮播文件的新增
     *
     */
    @AnonymousPostMapping("/uploadcommdity")
    public SysResult uploadoCommdity(MultipartFile file) throws IOException {

        ImageVO imageVO = fileService.upload(file);

        //说明业务执行有误
        if(imageVO != null){
            //获取网络地址
            commdityCarousel = commdityCarousel +imageVO.getUrlPath()+",";
        }else {
            return SysResult.fail();
        }

        return SysResult.success(imageVO);

    }

    /***
     *实现商品详情的新增
     */
    @AnonymousPostMapping("/uploaddetails")
    public SysResult uploaddetails(MultipartFile file) throws IOException {

        ImageVO imageVO = fileService.upload(file);

        //说明业务执行有误
        if(imageVO != null){
            //获取网络地址
            commdityDtails = commdityDtails+imageVO.getUrlPath()+",";
        }else {
            return SysResult.fail();
        }

        return SysResult.success(imageVO);

    }

    /***
     *实现首页图片的新增
     */
    @AnonymousPostMapping("/uploadUrlbody")
    public SysResult uploadUrlbody(MultipartFile file) throws IOException {

        ImageVO imageVO = fileService.upload(file);

        //说明业务执行有误
        if(imageVO != null){
            //获取网络地址
            body = body+imageVO.getUrlPath()+",";
        }else {
            return SysResult.fail();
        }

        return SysResult.success(imageVO);

    }
    @AnonymousPostMapping("/uploadUrltail")
    public SysResult uploadUrltail(MultipartFile file) throws IOException {

        ImageVO imageVO = fileService.upload(file);

        //说明业务执行有误
        if(imageVO != null){
            //获取网络地址
            tail = tail+imageVO.getUrlPath()+",";
        }else {
            return SysResult.fail();
        }

        return SysResult.success(imageVO);

    }
    @AnonymousPostMapping("/uploheader")
    public SysResult uplohome(MultipartFile file) throws IOException {

        ImageVO imageVO = fileService.upload(file);

        //说明业务执行有误
        if(imageVO != null){
            //获取网络地址
            header = header+imageVO.getUrlPath()+",";
        }else {
            return SysResult.fail();
        }

        return SysResult.success(imageVO);

    }



    /**
     * 业务: 删除图片
     * URL:  http://localhost:8091/file/deleteFile?virtualPath=xxxxx
     * 参数:  virtualPath 虚拟路径
     * 返回值: SysResult对象
     */
    @AnonymousDeleteMapping("/deleteFile")
    public SysResult deleteFile(String virtualPath){

        fileService.deleteFile(virtualPath);
        return SysResult.success();
    }

    //文件上传入门案例
    /*
    @PostMapping("/upload")
    public SysResult upload(MultipartFile file) throws IOException {
        //1.获取文件名称  a.jpg
        String fileName = file.getOriginalFilename();
        //2.准备文件目录
        String fileDir = "G:/images/";
        //2.1 判断目录是否存在
        File dir = new File(fileDir);
        if(!dir.exists()){
            //如果目录不存在,则创建多级目录
            dir.mkdirs();
        }
        //3.准备文件全路径
        String localPath = file  Dir + fileName;
        //4.实现文件输出
        file.transferTo(new File(localPath));
        System.out.println("文件上传成功!!!!");
        return SysResult.success();
    }
    */
}
